<!-- mod by luoji  all file -->
<template>
  <a-modal
    v-model="visible"
    :title="'发货'"
    :maskClosable="false"
    :keyboard="false"
    :confirmLoading="confirmLoading"
    :destroyOnClose="true"
    :width="700"
    @ok="ok"
    @cancel="close"
  > 
    <div class="popWindow" v-if="isRouterAlive||true">
      <a-row>
        <a-col :span="4">
          供应商
        </a-col>
        <a-col :span="20">
          <staff-selector
            v-model="user"
            :options="taffSelectorOpts"
          >
          </staff-selector>
        </a-col>
      </a-row>
      <a-row>
        <a-col :span="4">
          批号
        </a-col>
        <a-col :span="20">
          <a-input v-model="batchNumber"/>
        </a-col>
      </a-row>
      <a-row>
        <a-col :span="4">
          预计到货日期
        </a-col>
        <a-col :span="20">
          <a-date-picker v-model="date"/>
        </a-col>
      </a-row>
      <a-row class="popWindow_row popHeader">
        <a-col :span="7">物料编码</a-col>
        <a-col :span="7">物料名称</a-col>
        <a-col :span="7">数量</a-col>
        <a-col :span="3">
          <a-button type="primary" v-if="list.length < 10" @click="addLine">添加物料</a-button>
        </a-col>
      </a-row>
      <a-row class="popWindow_row" v-for="(item,index) in list" :key="index"> 
        <a-col :span="7">
          <a-input v-model="item.code"/>
        </a-col>
        <a-col :span="7">
          <a-input v-model="item.name"/>
        </a-col>
        <a-col :span="7">
          <a-input v-model="item.count"/>
        </a-col>
        <a-col :span="3">
          <a-button type="danger" @click="delLine(index)">删除行</a-button>
        </a-col>
      </a-row>
      <a-row>
        <a-col :span="24">
        </a-col>
      </a-row>
    </div>
  </a-modal>
</template>

<script lang="ts">

import {
  Component, Vue, Prop, Watch
} from 'vue-property-decorator';

import {
  Modal,
  Row,
  Col,
  Input,
  Select,
  Alert
} from '@h3/antd-vue';

import StaffSelector from "@cloudpivot/form/src/renderer/components/shared/staff-selector.vue";
import Axios from 'axios';
import { homeApi,userApi } from "@cloudpivot/api";

@Component({
  name: 'form-modify-send-goods',
  components: {
    AModal: Modal,
    ARow: Row,
    ACol: Col,
    Alert,
    ATextarea: Input.TextArea,
    ASelect: Select,
    ASelectOption: Select.Option,
    StaffSelector
  }
})
export default class FormSendGoodsModal extends Vue {

  user: any = null;

  @Prop() checkeds!: Array<any>;
  @Prop() dataSource!: Array<any>;

  list = []

  batchNumber: any = null;

  date: any = null;

  visible: boolean = false;

  confirmLoading: boolean = false;
  action: any = "";
  isRouterAlive: boolean = true;
  layout: any = {
    left: 6,
    right: 18
  }

  taffSelectorOpts: any = {
    selectOrg: false,
    selectUser: true,
    showModel: true,
    mulpitle: false,
    showSelect: true,
    placeholder: '请选择',
    title: '选择供应商'
  }
  
  addLine(){
    this.list.push({});
  }

  delLine(index){
    this.list.splice(index, 1);
  }

  async init(num: number, action: any) {
    this.list = [];
    if(this.checkeds && this.dataSource){
      for(var i = 0; i < this.checkeds.length; i++){
        if(this.checkeds[i]){
          this.list.push({
            code: this.dataSource[i].materialCode,
            name: this.dataSource[i].materialName,
            count: this.dataSource[i].count,
          })
        }
      }
    }
    console.log(this.checkeds,this.dataSource)
    this.action = action;
    var tmp;
    let user: any = window.sessionStorage.getItem('user');

      if (user) {

        user = JSON.parse(user);

      } else {

        user = { id:'',name:'' };

      }
      
      if(user.id != '' && user.id != null && user.id.length > 0){
        tmp = [{
            type: 1, //1部门，3人员
            imgUrl: '//www.baidu.com/img1.png',
            name:'',
            id: user.id,
            departmentId : '' ,
            departments : [] 
          }];
        var userinfo :any= await userApi.getUserInfo(user.id);
        if(userinfo && userinfo.errcode === 0) { 
          tmp[0].departmentId = userinfo.data.departmentId;
          tmp[0].name = userinfo.data.name;
          this.reload();
        }
      }
      this.user = tmp;
      var t = new Date();

      this.date = t.getFullYear() + '-' + (t.getMonth()+1) + '-' + t.getDate();
    this.show();
  }
  show() {
    this.visible = true;
  }

  close() {
    this.confirmLoading = false;
    this.visible = false;
  }

  ok() {
    //循环插入数据
    var idsStr=sessionStorage.getItem("sessionKey");
    var sessionCode=sessionStorage.getItem("sessionCode");
    let arr: any = null;
    if(idsStr){
        arr = idsStr.split(',');
    }
    var suppliers :any=[];
    var supplier:any = new Object();
    supplier.id = this.user[0].id; //供应商id
    suppliers.push(supplier);
    var ajaxurl="";
    switch(sessionCode)
    {
      case "CGDD":
        ajaxurl="/api/sendGoods/addShipments";
        break;
      case "":
        break;
    }
    Axios.post(ajaxurl,{
      finishedDate: this.date,
      ids: arr,
      suppliers: suppliers,
      batchNumber: this.batchNumber,
      }).then((res:any)=>{
        debugger;
        if(res.errmsg!="SUCCESS"){
          alert("订单信息生成失败");
        }else{
          this.close();
        }
      });
    console.log(this.date, this.user, this.batchNumber)
  }

  async getUserInfo(userId: string, item: any){
    const res: any = await userApi.getUserInfo(userId);
    if(res && res.errcode === 0) {
      item.b = res.data.name;
      item.c = res.data.mobile;
      this.reload();
    }else{
      this.$message.error(res.errmsg);
    }
  }

  reload(){
      this.isRouterAlive=false;
      this.$nextTick(function(){
        this.isRouterAlive=true
      })
    }
}
</script>

<style lang="less" scoped>
    .popWindow .ant-col{
      padding: 2px !important;
    }

    .popWindow .popHeader{
      margin-top: 10px;
      padding: 5px 8px;
      border-top: 1px solid #DDD;
    }

    .popWindow .popWindow_row{
      border-bottom: 1px solid #DDD;
    }
</style>
